package com.example.Controller;

import com.example.entity.Adminuser;
import com.example.service.AdminuserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.support.SessionStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginCtrl {
    //@Autowired
    @Resource
    AdminuserService adminuserService;

    @GetMapping("/toLoginPage")
    public String toLogin(){
        return "login";
    }

    @PostMapping("/dologin")
    public String doLogin(Adminuser adminuser, Model model, HttpSession session){
       Adminuser admin =  adminuserService.findAdmin(adminuser);

       if(admin == null){
           model.addAttribute("msg","用户民或者密码错误！");
           return "login";
       }else {
           session.setAttribute("aduName",admin.getAduname());
           return "redirect:/admin/goods";
       }
    }

    @GetMapping("/logout")
    public String doLogout(HttpSession session, SessionStatus sessionStatus){
        // 只是让session失效 但session仍然存在
        session.invalidate();
        sessionStatus.setComplete();
        return "login";
    }
}
